package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.OmsOrderGroupBuyItemMapper;
import com.fc.mall.mapper.OmsOrderGroupBuyMapper;
import com.fc.mall.mapper.PmsProductGroupBuyMapper;
import com.fc.mall.mapper.SmsGroupbuyPromotionMapper;
import com.fc.mall.model.*;
import com.fc.mall.portal.dao.GroupProductDao;
import com.fc.mall.portal.domain.GroupBuyProduct;
import com.fc.mall.portal.dto.GroupBuyerDto;
import com.fc.mall.portal.service.GroupProductService;
import com.fc.mall.portal.service.ProductService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.portal.vo.PmsProductVO;
import com.fc.mall.util.Utils;
import com.fc.mall.model.*;
import com.google.common.collect.Lists;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sunny on 2019/8/16.
 */

@Service
public class GroupProductServiceImpl implements GroupProductService {

    @Autowired
    private PmsProductGroupBuyMapper productGroupBuyMapper;

    @Autowired
    private GroupProductDao groupProductDao;

    @Autowired
    private ProductService productService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private OmsOrderGroupBuyMapper orderGroupBuyMapper;

    @Autowired
    private OmsOrderGroupBuyItemMapper orderGroupBuyItemMapper;

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private SmsGroupbuyPromotionMapper groupbuyPromotionMapper;

    @Override
    public List<GroupBuyProduct> listGroupProduct(int pageNum, int pageSize) {
        return groupProductDao.listGroupBuyProduct(pageSize, pageNum);
    }

    @Override
    public List<GroupBuyProduct> getGroupProductByPromotion(Long promotionId) {
        SmsGroupbuyPromotion groupbuyPromotion = groupbuyPromotionMapper.selectByPrimaryKey(promotionId);
        Date currentDate = new Date();

        if(groupbuyPromotion.getStartDate().before(currentDate) &&groupbuyPromotion.getEndDate().after(currentDate))
        {
            return groupProductDao.listGroupBuyProductByPromotionId(promotionId);
        }
        return Lists.newArrayList();
    }

    @Override
    public GroupBuyProduct getGroupProductById(Long id) {
        GroupBuyProduct groupBuyProduct = new GroupBuyProduct();
        PmsProductGroupBuy pmsProductGroupBuy = productGroupBuyMapper.selectByPrimaryKey(id);
        PmsProductVO pmsProduct = productService.getInfo(pmsProductGroupBuy.getProductId());
        mapperFacade.map(pmsProductGroupBuy, groupBuyProduct);
        mapperFacade.map(pmsProduct, groupBuyProduct);

        /**
         * 查询正在进行时的团。
         */
        OmsOrderGroupBuyExample orderGroupBuyExample = new OmsOrderGroupBuyExample();
        orderGroupBuyExample.createCriteria().andProductGroupIdEqualTo(id).andStatusEqualTo(1);
        orderGroupBuyExample.setOrderByClause("create_time desc");
        List<OmsOrderGroupBuy> orderGroupBuyList = orderGroupBuyMapper.selectByExample(orderGroupBuyExample);
        UmsMember currentMember = memberService.getCurrentMember();
        List<Long> orderBuyIds = new ArrayList<>();
        for(OmsOrderGroupBuy orderGroupBuy : orderGroupBuyList)
        {
            orderBuyIds.add(orderGroupBuy.getId());
            if(orderGroupBuy.getOwnerId().equals(currentMember.getId()))
            {
                groupBuyProduct.setHasGroup(true);
                groupBuyProduct.setOrderGroupId(orderGroupBuy.getId());
            }
            else
            {
                OmsOrderGroupBuyItemExample itemExample = new OmsOrderGroupBuyItemExample();
                itemExample.createCriteria().andOmsOrderGroupBuyIdEqualTo(orderGroupBuy.getId())
                .andMemberIdEqualTo(currentMember.getId());
                int count = orderGroupBuyItemMapper.countByExample(itemExample);
                if(count > 0)
                {
                    groupBuyProduct.setHasGroup(true);
                    groupBuyProduct.setOrderGroupId(orderGroupBuy.getId());
                }
            }
            if(groupBuyProduct.getGroupBuyerList().size() < 3 )
            {
                GroupBuyerDto groupBuyer = GroupBuyerDto.builder()
                        .groupBuyId(orderGroupBuy.getId())
                        .icon(orderGroupBuy.getIcon())
                        .nickname(Utils.urlDecoderTransfer(orderGroupBuy.getNickname()))
                        .residualCount(pmsProductGroupBuy.getGroupCount() - orderGroupBuy.getMemberCount())
                        .residualTime(orderGroupBuy.getExpiredTime())
                        .build();
                groupBuyProduct.getGroupBuyerList().add(groupBuyer);
            }
        }


        return groupBuyProduct;
    }

    @Override
    public PmsProductGroupBuy getGroupBuyById(Long id) {
        return productGroupBuyMapper.selectByPrimaryKey(id);
    }


}
